/**
 * @Title: TecentSmsImpl
 * @Package cn.com.yiqi.wsc.service.SMS.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/4/5 15:54
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.SMS.impl;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.authCode.AuthCodeDao;
import cn.com.yiqi.wsc.dao.authCode.criteria.AuthCodeCriteria;
import cn.com.yiqi.wsc.entity.authCode.AuthCode;
import cn.com.yiqi.wsc.service.SMS.SmsService;
import cn.com.yiqi.wsc.service.SMS.enumation.SmsCodeType;
import cn.com.yiqi.wsc.service.SMS.tecentSmsUtil.SmsSingleSender;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 *
 *@ClassName: TecentSmsImpl
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/4/5 15:54
 *
 */
@Service("xxtSmsService")
public class TecentSmsImpl implements SmsService {
    @Resource
    SmsSingleSender smsSingleSender;

    @Autowired
    AuthCodeDao authCodeDao;


    /**
     * 发送验证码
     * */
    @Override
    public Result SendCode(SmsCodeType type, String mobile){
        Result re = new Result();
        //查询验证码记录是否存在
        AuthCodeCriteria criteria = new AuthCodeCriteria();
        criteria.setType(type.getValue());
        criteria.setMobile(mobile);
        AuthCode entity = authCodeDao.selectByCriteria(criteria);
        if(null == entity){
            entity = new AuthCode();
        }else if(entity.getTimes() >= 5 && entity.getType() == SmsCodeType.bingMobile.getValue()){//绑定手机号只能发送五次
            return ResultUtil.fail(re,"绑定手机号只能发送五次！");
        }
        entity.setTimes(entity.getTimes()==null ? 1 : entity.getTimes() + 1);
        entity.setType(type.getValue());
        entity.setTel(mobile);
        String code = RandomStringUtils.randomNumeric(4);
        entity.setCode(code);
        entity.setSendTime(new Date());
        // 调用发送短信
        smsSingleSender.sendCode(mobile,code);
        if(entity.getId() == null){
            authCodeDao.insert(entity);
        }else {
            authCodeDao.update(entity);
        }
        return ResultUtil.success(re);
    }


    /**
     * 检验验证码
     * */
    public Boolean validateCode(SmsCodeType type,String mobile,String code){
        AuthCodeCriteria criteria = new AuthCodeCriteria();
        if(StringUtils.isEmpty(code)){
            return false;
        }
        criteria.setType(type.getValue());
        criteria.setMobile(mobile);
        criteria.setCode(code);
        //10分钟有效
        criteria.setLimitMinute(10);
        AuthCode entity = authCodeDao.selectByCriteria(criteria);
        //验证码只能校验一次
        if(null != entity){
            entity.setCode(null);
            authCodeDao.update(entity);
        }
        return entity != null;
    }


}
